﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using Common;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.UI;
using Debug = UnityEngine.Debug;

namespace Flag
{
    public class GameFlag : MonoBehaviour
    {
        public Dictionary<string, Sprite> imageDictionary = new Dictionary<string, Sprite>();
        public Image rawImage;
        public List<FlagBytes> flagList;

        public void setImage(String tag)
        {
            if(imageDictionary.ContainsKey(tag))
                rawImage.GetComponent<Image>().sprite = imageDictionary[tag];
        }
        
        public void Awake()
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            ReadFromBytes();
            //ReadFromFile();
            stopwatch.Stop();
            Debug.Log("Flag执行时间为：" + stopwatch.ElapsedMilliseconds);
        }

        public void SaveFlyBytesToJson()
        {
            
        }

        public void ReadFromBytes()
        {

            /*
            IFormatter serializer = new BinaryFormatter();
            FileStream loadFile = new FileStream(Application.streamingAssetsPath + "/temp/newflag.bin", FileMode.Open, FileAccess.Read);
            flagList = serializer.Deserialize(loadFile) as List<FlagBytes>;
            */
            FileStream fs = new FileStream(Application.streamingAssetsPath + "/temp/newflag.bin", FileMode.Open, FileAccess.Read);
            BinaryReader reader = new BinaryReader(fs);
            var byteArray = Helper.ReadAllBytes(reader);
            flagList = Serializer.Deserialize<List<FlagBytes>>(byteArray);

            foreach (var flagBytes in flagList)
            {
                var name = flagBytes.name;
                var bytes = flagBytes.bytes;
                int width=128;
                int height=128;
                Texture2D texture = new Texture2D(width, height);
                texture.LoadImage(bytes);
                imageDictionary.Add(name,Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero));
            }
            
        }

        public void ReadFromFile()
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            var dirPath = "D:/DC_DATA/temp/flags/";
            var directoryInfo = new DirectoryInfo(dirPath);

            flagList = new List<FlagBytes>();


            foreach (var fileInfo in directoryInfo.GetFiles())
            {
                String name = fileInfo.Name.Substring(0, fileInfo.Name.LastIndexOf(".")).ToUpper();
                FileStream fileStream = new FileStream(dirPath + fileInfo.Name, FileMode.Open, FileAccess.Read);
                fileStream.Seek(0, SeekOrigin.Begin);
                byte[] bytes = new byte[fileStream.Length];
                FlagBytes flagBytes = new FlagBytes();
                flagBytes.bytes = bytes;
                flagBytes.name = name;
                flagList.Add(flagBytes);
                fileStream.Read(bytes, 0, (int)fileStream.Length);
                fileStream.Close();
                fileStream.Dispose();
                fileStream = null;
                int width=128;
                int height=128;
                Texture2D texture = new Texture2D(width, height);
                texture.LoadImage(bytes);
                imageDictionary.Add(name,Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero));
            }
            FileStream fs = new FileStream(Application.streamingAssetsPath + "/temp/newflag.bin", FileMode.Create);
            

            var byteArray = Serializer.Serialize(flagList);
            BinaryWriter writer = new BinaryWriter(fs);
            writer.Write(byteArray);
            writer.Close();
            fs.Close();
            
            /*
            IFormatter serializer = new BinaryFormatter();
            serializer.Serialize(fs, flagList);
            fs.Close();
            */
            stopwatch.Stop();
            Debug.Log("Flag执行时间为：" + stopwatch.ElapsedMilliseconds);

        }
    }
}